package Lee_code;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author blh
 * @date 2024年08月08日 9:31
 */
public class Lee_107_88 {
    Deque<List<Integer>> res = new ArrayDeque<>();

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        Deque<TreeNode> treeNodes = new ArrayDeque<>();
        if (root == null) {
            return null;
        }
        treeNodes.offer(root);
        while (!treeNodes.isEmpty()) {
            int size = treeNodes.size();
            List<Integer> tempRes = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode temp = treeNodes.poll();
                if (temp.left != null) {
                    treeNodes.offer(temp.left);
                }
                if (temp.right != null) {
                    treeNodes.offer(temp.right);
                }

                tempRes.add(temp.val);
            }
            res.push(tempRes);
        }
        return new ArrayList<>(res);
    }
}
